package com.cloud.phone.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.cloud.phone.tools.CmdTool;
import com.cloud.phone.view.TsUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Base64;

public class BingTopDama {

    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String getImageStr(String imgFile) {
        /**
         * @Description: 根据图片地址转换为base64编码字符串
         * @return String
         */
        FileInputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Base64.Encoder encoder = Base64.getEncoder();
        String encodedata = encoder.encodeToString(data);
        String encode="";
        try {
            encode = URLEncoder.encode(encodedata,"UTF-8");
        } catch (Exception e) {
            System.out.println("转码异常!"+e);
            e.printStackTrace();
        }
        return encode;
    }


    public static String sendPost(String url, String param,String charset) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        String line;
        StringBuffer sb=new StringBuffer();
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性 设置请求格式
            conn.setRequestProperty("contentType", charset);
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            //设置超时时间
            conn.setConnectTimeout(60*1000);
            conn.setReadTimeout(60*1000);

            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8"));
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应    设置接收格式
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(),charset));
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            result=sb.toString();
        } catch (Exception e) {
            System.out.println("发送 POST请求出现异常!"+e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }

    public static File getRootDirectory() {
        File directory =new File("/sdcard/DCIM");
        if (!directory.exists()) {
            directory=new File("/storage/emulated/0/DCIM");
        }
        return directory;
    }


    public static Bitmap takeScreenshotAndSaveToFilePath(String fileName) throws InterruptedException {
        String cmd = "screencap -p "+getRootDirectory().getAbsolutePath()+"/" + fileName + ".jpg";
        CmdTool.getInstance().doCmd(cmd);
        Thread.sleep(2000);
        String path = getRootDirectory().getAbsolutePath()+"/" + fileName + ".jpg";
        try {
            File file = new File(path);
            if (file.exists()) {
                Bitmap bitmap = BitmapFactory.decodeFile(path);
                if(bitmap!=null){
                    return bitmap;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    public static void saveBitmapToFile(Bitmap bitmap, String filePath) {
        try {
            FileOutputStream fos = new FileOutputStream(filePath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            TsUtils.showTips("保存文件成功");
        } catch (IOException e) {
            e.printStackTrace();
            TsUtils.showTips("保存文件失败"+e.getCause()+"/"+e.getMessage());
        }
    }

}